package com.media.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.media.AlbumManageUtils;
import com.media.R;
import com.media.image.ImageModel;
import com.media.image.ImageWork;
import com.media.revision.RevisionMediaActivity;
import com.media.revision.adapter.RevisionAdapter;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.Locale;

/**
 * @author 关忆北、
 * @description 视频
 * @time 2022/4/18 6:09 下午
 */

public class VideoFragment extends Fragment {
    ArrayList<ImageModel> list;
    private GridView gvVideo;
    private RevisionMediaActivity mActivity;
    private RevisionAdapter mAdapter;

    public VideoFragment() {
    }

    public VideoFragment(RevisionMediaActivity activity, ImageWork mImageWork, String mediaType) {
        this.mActivity = activity;
        mAdapter = new RevisionAdapter(mActivity, mImageWork, mediaType);
        StringBuilder mFormatBuilder = new StringBuilder();
        Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
        mAdapter.setmFormatBuilder(mFormatBuilder);
        mAdapter.setmFormatter(mFormatter);
    }

    public void setOnSelectClickListener(final RevisionAdapter.OnSelectClickListener onSelectClickListener) {
        mAdapter.setOnSelectClickListener(new RevisionAdapter.OnSelectClickListener() {
            @Override
            public void onSelectClick(int i, ImageModel imageModel, String type) {
                if (onSelectClickListener != null)
                    onSelectClickListener.onSelectClick(i, imageModel, type);

            }
        });
    }

    public void upAdapterSelectState() {
        if (mActivity.selectImageList.size() > 0) {
            mAdapter.setSelectType(AlbumManageUtils.getSelectType(mActivity.selectImageList.get(0)));
        } else {
            mAdapter.setSelectType(0);
        }
        mActivity.selectImageList = AlbumManageUtils.setSelectSort(mActivity.selectImageList);
        mAdapter.setSelectImageList(mActivity.selectImageList);
        mAdapter.setItemImageList(mActivity.flVideoList);
        mAdapter.notifyDataSetChanged();
    }

    public void setOnPreviewClickListener(RevisionAdapter.OnPreviewClickListener onPreviewClickListener) {
        if (mAdapter != null) mAdapter.setOnPreviewClickListener(onPreviewClickListener);
    }


    public void setList(ArrayList<ImageModel> list) {
        this.list = list;
        if (mAdapter != null) {
            mAdapter.setItemImageList(list);
            mAdapter.notifyDataSetChanged();
        }
    }

    public void addData(ArrayList<ImageModel> list) {
        this.list.addAll(list);
        if (mAdapter != null)
            mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_media_video, container, false);
        gvVideo = (GridView) inflate.findViewById(R.id.gv_video);

        gvVideo.setAdapter(mAdapter);
        return inflate;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
}